package com.government.company.service.impl;

import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.government.area.domain.ParkArea;
import com.government.common.core.domain.entity.SysUser;
import com.government.common.exception.ServiceException;
import com.government.common.utils.StringUtils;
import com.government.company.bo.CompanyExcel;
import com.government.company.bo.UserCompanyExcel;
import com.government.company.domain.Company;
import com.government.company.domain.Industry;
import com.government.company.domain.UserCompany;
import com.government.company.domain.UserCompanyLog;
import com.government.company.mapper.CompanyMapper;
import com.government.company.mapper.UserCompanyLogMapper;
import com.government.company.mapper.UserCompanyMapper;
import com.government.company.service.ICompanyService;
import com.government.company.service.IUserCompanyService;
import org.apache.commons.compress.utils.Lists;
import org.springframework.transaction.annotation.Transactional;
import com.government.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static com.government.common.utils.SecurityUtils.getLoginUser;

/**
 * 用户企业Service业务层处理
 *
 * @author system
 * @date 2024-09-27
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserCompanyServiceImpl implements IUserCompanyService {
    @Autowired
    private UserCompanyMapper userCompanyMapper;

    @Autowired
    private UserCompanyLogMapper logMapper;

    @Autowired
    private CompanyMapper companyMapper;

    @Autowired
    private ICompanyService companyService;

    @Resource(name = "serviceThreadPool")
    private ExecutorService executorService;

    /**
     * 查询用户企业
     *
     * @param id 用户企业主键
     * @return 用户企业
     */
    @Override
    public UserCompany selectUserCompanyById(Long id) {
        return userCompanyMapper.selectUserCompanyById(id);
    }

    /**
     * 查询用户企业列表
     *
     * @param userCompany 用户企业
     * @return 用户企业
     */
    @Override
    public List<UserCompany> selectUserCompanyList(UserCompany userCompany) {
        return userCompanyMapper.selectUserCompanyList(userCompany);
    }

    /**
     * 新增用户企业
     *
     * @param userCompany 用户企业
     * @return 结果
     */
    @Override
    public int insertUserCompany(UserCompany userCompany) {
        userCompany.setCreateTime(DateUtils.getNowDate());
        return userCompanyMapper.insertUserCompany(userCompany);
    }

    /**
     * 修改用户企业
     *
     * @param userCompany 用户企业
     * @return 结果
     */
    @Override
    public int updateUserCompany(UserCompany userCompany) {
        userCompany.setUpdateTime(DateUtils.getNowDate());
        return userCompanyMapper.updateUserCompany(userCompany);
    }

    /**
     * 批量删除用户企业
     *
     * @param ids 需要删除的用户企业主键
     * @return 结果
     */
    @Override
    public int deleteUserCompanyByIds(Long[] ids) {
        return userCompanyMapper.deleteUserCompanyByIds(ids);
    }

    /**
     * 删除用户企业信息
     *
     * @param id 用户企业主键
     * @return 结果
     */
    @Override
    public int deleteUserCompanyById(Long id) {
        return userCompanyMapper.deleteUserCompanyById(id);
    }

    @Override
    public void importUserCompanyData(List<UserCompanyExcel> userCompanyList) {

        SysUser user = getLoginUser().getUser();
        if (StringUtils.isNull(userCompanyList) || userCompanyList.size() == 0) {
            throw new ServiceException("导入数据不能为空！");
        }


        executorService.execute(() -> {

            for (UserCompanyExcel companyExcel : userCompanyList) {
                Company company = companyMapper.selectOne(Wrappers.<Company>lambdaQuery().eq(Company::getName, companyExcel.getCompanyName()));
                if (Objects.isNull(company)) {
                    company = companyService.getQccCompany(companyExcel.getCompanyName());
                }
                UserCompany userCompany = userCompanyMapper.selectOne(Wrappers.<UserCompany>lambdaQuery()
                        .eq(UserCompany::getYear,companyExcel.getYear())
                        .eq(UserCompany::getCompanyName,companyExcel.getCompanyName()));
                if(Objects.nonNull(userCompany)) {
                    userCompany = new UserCompany();
                }
                userCompany.setCompanyName(companyExcel.getCompanyName());
                userCompany.setUserId(user.getUserId());
                userCompany.setCompanyId(company.getId());
                userCompany.setYear(companyExcel.getYear());
                List<UserCompanyLog> userCompanyLogs = new ArrayList<>();

                // 主营收入
                if(Objects.nonNull(companyExcel.getIncome())&&Objects.nonNull(userCompany.getIncome())
                && !companyExcel.getIncome().equals(userCompany.getIncome())) {
                    UserCompanyLog userCompanyLog = new UserCompanyLog();
                    userCompanyLog.setFieldName("主营收入");
                    userCompanyLog.setBefore(String.valueOf(userCompany.getIncome()));
                    userCompanyLog.setAfter(String.valueOf(companyExcel.getIncome()));
                    userCompanyLog.setCreateBy(String.valueOf(user.getUserId()));
                    userCompanyLogs.add(userCompanyLog);
                }
                userCompany.setIncome(companyExcel.getIncome());

                // 研发费用-报税
                if(Objects.nonNull(companyExcel.getDeclareTaxes())&&Objects.nonNull(userCompany.getDeclareTaxes())
                        && !companyExcel.getDeclareTaxes().equals(userCompany.getDeclareTaxes())){
                    UserCompanyLog userCompanyLog = new UserCompanyLog();
                    userCompanyLog.setFieldName("研发费用-报税");
                    userCompanyLog.setBefore(String.valueOf(userCompany.getDeclareTaxes()));
                    userCompanyLog.setAfter(String.valueOf(companyExcel.getDeclareTaxes()));
                    userCompanyLog.setCreateBy(String.valueOf(user.getUserId()));
                    userCompanyLogs.add(userCompanyLog);
                }
                userCompany.setDeclareTaxes(companyExcel.getDeclareTaxes());

                // 研发费用-加计扣除
                if(Objects.nonNull(companyExcel.getTakeOut())&&Objects.nonNull(userCompany.getTakeOut())
                        && !companyExcel.getTakeOut().equals(userCompany.getTakeOut())){
                    UserCompanyLog userCompanyLog = new UserCompanyLog();
                    userCompanyLog.setFieldName("研发费用-加计扣除");
                    userCompanyLog.setBefore(String.valueOf(userCompany.getTakeOut()));
                    userCompanyLog.setAfter(String.valueOf(companyExcel.getTakeOut()));
                    userCompanyLog.setCreateBy(String.valueOf(user.getUserId()));
                    userCompanyLogs.add(userCompanyLog);
                }
                userCompany.setTakeOut(companyExcel.getTakeOut());

                // 主营产品/服务
                if(Objects.nonNull(companyExcel.getProduct())&&Objects.nonNull(userCompany.getProduct())
                        && !companyExcel.getProduct().equals(userCompany.getProduct())){
                    UserCompanyLog userCompanyLog = new UserCompanyLog();
                    userCompanyLog.setFieldName("主营产品/服务");
                    userCompanyLog.setBefore(userCompany.getProduct());
                    userCompanyLog.setAfter(companyExcel.getProduct());
                    userCompanyLog.setCreateBy(String.valueOf(user.getUserId()));
                    userCompanyLogs.add(userCompanyLog);
                }
                userCompany.setProduct(companyExcel.getProduct());

                // 所属产业链
                if(Objects.nonNull(companyExcel.getIndustrialChain())&&Objects.nonNull(userCompany.getIndustrialChain())
                        && !companyExcel.getIndustrialChain().equals(userCompany.getIndustrialChain())){
                    UserCompanyLog userCompanyLog = new UserCompanyLog();
                    userCompanyLog.setFieldName("所属产业链");
                    userCompanyLog.setBefore(userCompany.getIndustrialChain());
                    userCompanyLog.setAfter(companyExcel.getIndustrialChain());
                    userCompanyLog.setCreateBy(String.valueOf(user.getUserId()));
                    userCompanyLogs.add(userCompanyLog);
                }
                userCompany.setIndustrialChain(companyExcel.getIndustrialChain());

                // 办公地址
                if(Objects.nonNull(companyExcel.getAddress())&&Objects.nonNull(userCompany.getAddress())
                        && !companyExcel.getAddress().equals(userCompany.getAddress())){
                    UserCompanyLog userCompanyLog = new UserCompanyLog();
                    userCompanyLog.setFieldName("办公地址");
                    userCompanyLog.setBefore(userCompany.getAddress());
                    userCompanyLog.setAfter(companyExcel.getAddress());
                    userCompanyLog.setCreateBy(String.valueOf(user.getUserId()));
                    userCompanyLogs.add(userCompanyLog);
                }
                userCompany.setAddress(companyExcel.getAddress());

                // 所属片区
                if(Objects.nonNull(companyExcel.getDistrict())&&Objects.nonNull(userCompany.getDistrict())
                        && !companyExcel.getDistrict().equals(userCompany.getDistrict())){
                    UserCompanyLog userCompanyLog = new UserCompanyLog();
                    userCompanyLog.setFieldName("所属片区");
                    userCompanyLog.setBefore(userCompany.getDistrict());
                    userCompanyLog.setAfter(companyExcel.getDistrict());
                    userCompanyLog.setCreateBy(String.valueOf(user.getUserId()));
                    userCompanyLogs.add(userCompanyLog);
                }
                userCompany.setDistrict(companyExcel.getDistrict());

                // 所属楼栋
                if(Objects.nonNull(companyExcel.getTower())&&Objects.nonNull(userCompany.getTower())
                        && !companyExcel.getTower().equals(userCompany.getTower())){
                    UserCompanyLog userCompanyLog = new UserCompanyLog();
                    userCompanyLog.setFieldName("所属楼栋");
                    userCompanyLog.setBefore(userCompany.getTower());
                    userCompanyLog.setAfter(companyExcel.getTower());
                    userCompanyLog.setCreateBy(String.valueOf(user.getUserId()));
                    userCompanyLogs.add(userCompanyLog);
                }
                userCompany.setTower(companyExcel.getTower());

                // 所属楼层
                if(Objects.nonNull(companyExcel.getFloor())&&Objects.nonNull(userCompany.getFloor())
                        && !companyExcel.getFloor().equals(userCompany.getFloor())){
                    UserCompanyLog userCompanyLog = new UserCompanyLog();
                    userCompanyLog.setFieldName("所属楼层");
                    userCompanyLog.setBefore(userCompany.getFloor());
                    userCompanyLog.setAfter(companyExcel.getFloor());
                    userCompanyLog.setCreateBy(String.valueOf(user.getUserId()));
                    userCompanyLogs.add(userCompanyLog);
                }
                userCompany.setFloor(companyExcel.getFloor());

                // 所属房号
                if(Objects.nonNull(companyExcel.getRoom())&&Objects.nonNull(userCompany.getRoom())
                        && !companyExcel.getRoom().equals(userCompany.getRoom())){
                    UserCompanyLog userCompanyLog = new UserCompanyLog();
                    userCompanyLog.setFieldName("所属房号");
                    userCompanyLog.setBefore(userCompany.getRoom());
                    userCompanyLog.setAfter(companyExcel.getRoom());
                    userCompanyLog.setCreateBy(String.valueOf(user.getUserId()));
                    userCompanyLogs.add(userCompanyLog);
                }
                userCompany.setRoom(companyExcel.getRoom());

                // 总体面积
                if(Objects.nonNull(companyExcel.getArea())&&Objects.nonNull(userCompany.getArea())
                        && !companyExcel.getArea().equals(userCompany.getArea())){
                    UserCompanyLog userCompanyLog = new UserCompanyLog();
                    userCompanyLog.setFieldName("总体面积");
                    userCompanyLog.setBefore(userCompany.getArea());
                    userCompanyLog.setAfter(companyExcel.getArea());
                    userCompanyLog.setCreateBy(String.valueOf(user.getUserId()));
                    userCompanyLogs.add(userCompanyLog);
                }
                userCompany.setArea(companyExcel.getArea());

                // 四上类型
                if(Objects.nonNull(companyExcel.getQuadritype())&&Objects.nonNull(userCompany.getQuadritype())
                        && !companyExcel.getQuadritype().equals(userCompany.getQuadritype())){
                    UserCompanyLog userCompanyLog = new UserCompanyLog();
                    userCompanyLog.setFieldName("四上类型");
                    userCompanyLog.setBefore(userCompany.getQuadritype());
                    userCompanyLog.setAfter(companyExcel.getQuadritype());
                    userCompanyLog.setCreateBy(String.valueOf(user.getUserId()));
                    userCompanyLogs.add(userCompanyLog);
                }
                userCompany.setQuadritype(companyExcel.getQuadritype());

                // 客户等级
                if(Objects.nonNull(companyExcel.getCustomerLevel())&&Objects.nonNull(userCompany.getCustomerLevel())
                        && !companyExcel.getCustomerLevel().equals(userCompany.getCustomerLevel())){
                    UserCompanyLog userCompanyLog = new UserCompanyLog();
                    userCompanyLog.setFieldName("客户等级");
                    userCompanyLog.setBefore(userCompany.getCustomerLevel());
                    userCompanyLog.setAfter(companyExcel.getCustomerLevel());
                    userCompanyLog.setCreateBy(String.valueOf(user.getUserId()));
                    userCompanyLogs.add(userCompanyLog);
                }
                userCompany.setCustomerLevel(companyExcel.getCustomerLevel());

                // 客户类型
                if(Objects.nonNull(companyExcel.getCustomerType())&&Objects.nonNull(userCompany.getCustomerType())
                        && !companyExcel.getCustomerLevel().equals(userCompany.getCustomerType())){
                    UserCompanyLog userCompanyLog = new UserCompanyLog();
                    userCompanyLog.setFieldName("客户类型");
                    userCompanyLog.setBefore(userCompany.getCustomerType());
                    userCompanyLog.setAfter(companyExcel.getCustomerType());
                    userCompanyLog.setCreateBy(String.valueOf(user.getUserId()));
                    userCompanyLogs.add(userCompanyLog);
                }
                userCompany.setCustomerType(companyExcel.getCustomerType());

                // 姓名
                if(Objects.nonNull(companyExcel.getName())&&Objects.nonNull(userCompany.getName())
                        && !companyExcel.getName().equals(userCompany.getName())){
                    UserCompanyLog userCompanyLog = new UserCompanyLog();
                    userCompanyLog.setFieldName("姓名");
                    userCompanyLog.setBefore(userCompany.getName());
                    userCompanyLog.setAfter(companyExcel.getName());
                    userCompanyLog.setCreateBy(String.valueOf(user.getUserId()));
                    userCompanyLogs.add(userCompanyLog);
                }
                userCompany.setName(companyExcel.getName());

                // 职务
                if(Objects.nonNull(companyExcel.getJob())&&Objects.nonNull(userCompany.getJob())
                        && !companyExcel.getJob().equals(userCompany.getJob())){
                    UserCompanyLog userCompanyLog = new UserCompanyLog();
                    userCompanyLog.setFieldName("职务");
                    userCompanyLog.setBefore(userCompany.getJob());
                    userCompanyLog.setAfter(companyExcel.getJob());
                    userCompanyLog.setCreateBy(String.valueOf(user.getUserId()));
                    userCompanyLogs.add(userCompanyLog);
                }
                userCompany.setJob(companyExcel.getJob());

                // 主管事项
                if(Objects.nonNull(companyExcel.getItem())&&Objects.nonNull(userCompany.getItem())
                        && !companyExcel.getItem().equals(userCompany.getItem())){
                    UserCompanyLog userCompanyLog = new UserCompanyLog();
                    userCompanyLog.setFieldName("主管事项");
                    userCompanyLog.setBefore(userCompany.getItem());
                    userCompanyLog.setAfter(companyExcel.getItem());
                    userCompanyLog.setCreateBy(String.valueOf(user.getUserId()));
                    userCompanyLogs.add(userCompanyLog);
                }
                userCompany.setItem(companyExcel.getItem());

                // 手机号码
                if(Objects.nonNull(companyExcel.getPhone())&&Objects.nonNull(userCompany.getPhone())
                        && !companyExcel.getPhone().equals(userCompany.getPhone())){
                    UserCompanyLog userCompanyLog = new UserCompanyLog();
                    userCompanyLog.setFieldName("手机号码");
                    userCompanyLog.setBefore(userCompany.getPhone());
                    userCompanyLog.setAfter(companyExcel.getPhone());
                    userCompanyLog.setCreateBy(String.valueOf(user.getUserId()));
                    userCompanyLogs.add(userCompanyLog);
                }
                userCompany.setPhone(companyExcel.getPhone());

                // 座机号码
                if(Objects.nonNull(companyExcel.getTel())&&Objects.nonNull(userCompany.getTel())
                        && !companyExcel.getTel().equals(userCompany.getTel())){
                    UserCompanyLog userCompanyLog = new UserCompanyLog();
                    userCompanyLog.setFieldName("座机号码");
                    userCompanyLog.setBefore(userCompany.getTel());
                    userCompanyLog.setAfter(companyExcel.getTel());
                    userCompanyLog.setCreateBy(String.valueOf(user.getUserId()));
                    userCompanyLogs.add(userCompanyLog);
                }
                userCompany.setTel(companyExcel.getTel());

                // 邮箱
                if(Objects.nonNull(companyExcel.getEmail())&&Objects.nonNull(userCompany.getEmail())
                        && !companyExcel.getEmail().equals(userCompany.getEmail())){
                    UserCompanyLog userCompanyLog = new UserCompanyLog();
                    userCompanyLog.setFieldName("邮箱");
                    userCompanyLog.setBefore(userCompany.getEmail());
                    userCompanyLog.setAfter(companyExcel.getEmail());
                    userCompanyLog.setCreateBy(String.valueOf(user.getUserId()));
                    userCompanyLogs.add(userCompanyLog);
                }
                userCompany.setTel(companyExcel.getEmail());

                // 岗位状态
                if(Objects.nonNull(companyExcel.getPostState())&&Objects.nonNull(userCompany.getPostState())
                        && !companyExcel.getPostState().equals(userCompany.getPostState())){
                    UserCompanyLog userCompanyLog = new UserCompanyLog();
                    userCompanyLog.setFieldName("岗位状态");
                    userCompanyLog.setBefore(userCompany.getPostState());
                    userCompanyLog.setAfter(companyExcel.getPostState());
                    userCompanyLog.setCreateBy(String.valueOf(user.getUserId()));
                    userCompanyLogs.add(userCompanyLog);
                }
                userCompany.setTel(companyExcel.getPostState());

                if (Objects.isNull(userCompany.getId())) {
                    userCompanyMapper.insert(userCompany);
                } else {
                    userCompany.setUpdateTime(new Date());
                    userCompany.setUpdateBy(String.valueOf(user.getUserId()));
                    userCompanyMapper.updateById(userCompany);
                }

            }
        });
    }
}
